/******************************************************************************
 *
 * Copyright:
 *    (C) 2005 Embedded Artists AB
 *
 * File:
 *    uart.h
 *
 * Description:
 *    Contains interface definitions for the polled UART implementation
 *
 *****************************************************************************/
#ifndef _UART_H_
#define _UART_H_

/******************************************************************************
 * Includes
 *****************************************************************************/
#include "../Startup/general.h"
#include "../Startup/lpc2xxx.h"

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/
//bit definitions in LCR and FCR registers in the UART
#define ULCR_CHAR_7   0x02
#define ULCR_CHAR_8   0x03
#define ULCR_PAR_NO   0x00
#define ULCR_PAR_EVEN 0x30
#define ULCR_PAR_ODD  0x10
#define ULCR_STOP_1   0x00
#define ULCR_STOP_2   0x04

#define UFCR_FIFO_ENABLE 0x01
#define UFCR_FIFO_TRIG1  0x00
#define UFCR_FIFO_TRIG4  0x40
#define UFCR_FIFO_TRIG8  0x80
#define UFCR_FIFO_TRIG16 0xc0

//define for determning the correct uart clock division factor
//Note that the expressions should always be constants and fully evaluated at
//compile time, else a lot of code will be generated.
#define UART_BPS(pclk,bps) (tU16)((pclk / ((bps) * 16.0)) + 0.5)

//definitions for some common bitrates
#define B1200(pclk)         UART_BPS(pclk,1200)
#define B2400(pclk)         UART_BPS(pclk,2400)
#define B9600(pclk)         UART_BPS(pclk,9600)
#define B19200(pclk)        UART_BPS(pclk,19200)
#define B38400(pclk)        UART_BPS(pclk,38400)
#define B57600(pclk)        UART_BPS(pclk,57600)
#define B115200(pclk)       UART_BPS(pclk,115200)

//definitions for mode settings
#define UART_7N1      (tU8)(ULCR_CHAR_7 + ULCR_PAR_NO   + ULCR_STOP_1)
#define UART_7N2      (tU8)(ULCR_CHAR_7 + ULCR_PAR_NO   + ULCR_STOP_2)
#define UART_7E1      (tU8)(ULCR_CHAR_7 + ULCR_PAR_EVEN + ULCR_STOP_1)
#define UART_7E2      (tU8)(ULCR_CHAR_7 + ULCR_PAR_EVEN + ULCR_STOP_2)
#define UART_7O1      (tU8)(ULCR_CHAR_7 + ULCR_PAR_ODD  + ULCR_STOP_1)
#define UART_7O2      (tU8)(ULCR_CHAR_7 + ULCR_PAR_ODD  + ULCR_STOP_2)
#define UART_8N1      (tU8)(ULCR_CHAR_8 + ULCR_PAR_NO   + ULCR_STOP_1)
#define UART_8N2      (tU8)(ULCR_CHAR_8 + ULCR_PAR_NO   + ULCR_STOP_2)
#define UART_8E1      (tU8)(ULCR_CHAR_8 + ULCR_PAR_EVEN + ULCR_STOP_1)
#define UART_8E2      (tU8)(ULCR_CHAR_8 + ULCR_PAR_EVEN + ULCR_STOP_2)
#define UART_8O1      (tU8)(ULCR_CHAR_8 + ULCR_PAR_ODD  + ULCR_STOP_1)
#define UART_8O2      (tU8)(ULCR_CHAR_8 + ULCR_PAR_ODD  + ULCR_STOP_2)

//definitions for FIFO control settings
#define UART_FIFO_OFF (0x00)
#define UART_FIFO_1   (tU8)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG1)
#define UART_FIFO_4   (tU8)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG4)
#define UART_FIFO_8   (tU8)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG8)
#define UART_FIFO_14  (tU8)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG14)

//XModem data
#define soh 0x01
#define	eot 0x04
#define ack 0x06
#define	nak 0x15
#define	can 0x18
#define	c 0x43

struct packet
{
	char startOfHeader;
	tU8 packetNum;
	tU8 packetNumCompl;
	char fileData[128];
	tU8 checkSum1;
	tU8 checkSum2;
};

struct packet dataToSend;

tBool useCRC;
tU8 initSign;
tU16 packetNumber;

tU16 crc;
tU8 checksum;
tU32 stopPoint;
tBool eof;


void initUart0(tU16 div_factor, tU8 mode, tU8 fifo_mode);
void initUart0_96008N1_FifoOff();
void uart0SendChar(tU8 charToSend);
void uart0SendCh(tU8 charToSend);
void uart0SendString(tU8 *pString);
void uart0SendChars(char *pBuff, tU16 count);
tU8 uart0TxEmpty(void);
void uart0TxFlush(void);
tU8 uart0GetCh(void);
tU8 uart0GetChar(/*tU8 *pRxChar*/);
void uart0SetPinsel();

void Uart0ClrFifo();
void uart0SendBuffer(tU8* pBuff, tU16 count);

//XModem transmit functions;
tBool XmSendData(tU8* data, tU32 bytes);
void XmResetPacket();
void XmMakePacket(tU8* data);
tU16 XmCRC();
tU8 XmCheckSum();

#endif
